package com.example.md.common;

import lombok.Data;

import java.io.Serializable;

import com.example.md.common.enums.BaseError;

@Data
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private int code;

    /**
     * 消息
     */
    private String msg;

    /**
     * 数据
     */
    private T data;

    public Result() {
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功
     */
    public static <T> Result<T> success() {
        return new Result<>(200, "success");
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(200, "success", data);
    }

    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(200, msg, data);
    }

    /**
     * 失败
     */
    public static <T> Result<T> failed() {
        return new Result<>(400, "failed");
    }

    public static <T> Result<T> failed(String msg) {
        return new Result<>(400, msg);
    }

    public static <T> Result<T> failed(int code, String msg) {
        return new Result<>(code, msg);
    }

    public static <T> Result<T> failed(BaseError error) {
        return new Result<>(error.code(), error.message());
    }
}
